from tipsz8 import saveLocalData, loadLocalData, clear_cache, getFileUTime
from collections import defaultdict

allshop = loadLocalData("shop.json") or []
p = ["3000-5000", "5000-8000", "8000-12000", "12000-20000", "20000-25000", "25000-100000"]
useplan = ["主流游戏", "3A大作游戏", "办公学习", "创意设计", "视频渲染", "AI本地部署"]
cpu = ["Ryzen 5", "Ryzen 7", "Ryzen 9", "Core Ultra","i5", "i7", "i9","i3"]
gpu = ["RTX30系", "RTX40系", "RTX50系", "RX6000系", "RX7000系", "RX9000系",]

# Group shops by price range
price_groups = defaultdict(list)
for shop in allshop:
    price = shop.get("价格", 0)
    for price_range in p:
        min_price, max_price = map(int, price_range.split('-'))
        if min_price <= price <= max_price:
            price_groups[price_range].append(shop)    
            for plan in useplan:
                if plan in shop.get("用途", []):
                    price_groups[f"{price_range} {plan}"].append(shop)
                    for c in cpu:
                        if c in shop.get("cpu类型", []):
                            price_groups[f"{price_range} {plan} {c}"].append(shop)
                            for g in gpu:
                                g_text = shop.get("gpu类型", "")
                                g_text = g_text.replace(" ", "")
                                # print(g_text)
                                if g in g_text:
                                    price_groups[f"{price_range} {plan} {c} {g}"].append(shop)
                                    break
                    for g in gpu:
                        g_text = shop.get("gpu类型", "")
                        g_text = g_text.replace(" ", "")
                        # print(g_text)
                        if g in g_text:
                            price_groups[f"{price_range} {plan} {g}"].append(shop)
                            break
            
            for c in cpu:
                if c in shop.get("cpu类型", []):
                    price_groups[f"{price_range} {c}"].append(shop)
                    for g in gpu:
                        g_text = shop.get("gpu类型", "")
                        g_text = g_text.replace(" ", "")
                        if g in g_text:
                            price_groups[f"{price_range} {c} {g}"].append(shop)
                            break
            for g in gpu:
                g_text = shop.get("gpu类型", "")
                g_text = g_text.replace(" ", "")
                if g in g_text:
                    price_groups[f"{price_range} {g}"].append(shop)
                    break
    
    
    
    
use_groups = defaultdict(list)

for plan in useplan:
    for shop in allshop:
        u = shop.get("用途", "")
        if plan in u:
            use_groups[plan].append(shop)
            for c in cpu:
                if c in shop.get("cpu类型", []):
                    use_groups[f"{plan} {c}"].append(shop)
                    for g in gpu:
                        g_text = shop.get("gpu类型", "")
                        g_text = g_text.replace(" ", "")
                        if g in g_text:
                            use_groups[f"{plan} {c} {g}"].append(shop)
            for g in gpu:
                g_text = shop.get("gpu类型", "")
                g_text = g_text.replace(" ", "")
                if g in g_text:
                    use_groups[f"{plan} {g}"].append(shop)


c_groups = defaultdict(list)
for c in cpu:
    for shop in allshop:
        if c in shop.get("cpu类型", []):
            c_groups[c].append(shop)
            for g in gpu:
                g_text = shop.get("gpu类型", "")
                g_text = g_text.replace(" ", "")
                if g in g_text:
                    c_groups[f"{c} {g}"].append(shop)

g_groups = defaultdict(list)
for g in gpu:
    for shop in allshop:
        g_text = shop.get("gpu类型", "")
        g_text = g_text.replace(" ", "")
        if g in g_text:
            g_groups[g].append(shop)

saveLocalData(price_groups, 'p_groups.json')
saveLocalData(use_groups, 'u_groups.json')
saveLocalData(c_groups, 'c_groups.json')
saveLocalData(g_groups, 'g_groups.json')